<?php 
interface I_parse_bank_statement {
	function __construct();
	public function read_file();
}
class parse_bank_statement_base implements I_parse_bank_statement
{

	protected $transaction = array();
  protected $data = array();
	
   function __construct() 
    { 
    require_once "../Excel/reader.php";
    // ExcelFile($filename, $encoding);
    $filename =   "./ListaMovimenti.xls";
    $this->data = new Spreadsheet_Excel_Reader();
    // Set output Encoding.
    $this->data->setOutputEncoding('CP1251');
    
    
    $this->data->read($filename);
    
    error_reporting(E_ALL ^ E_NOTICE);
	    
	 }
	
	 public function read_file() {}
	 
	 public function add_transaction(){
			 
		 }
   public function get_transaction(){
    return $this->transaction;
			 
		 }
	 

	
}
class parse_bank_statement_intesa_sanpaolo extends parse_bank_statement_base {
    private function xl2timestamp($xl_date) {
		  $timestamp =  date('Y-m-d',((int) $xl_date - 25569)*86400) ;
      return $timestamp;
		}
    
		public function read_content(){
		 		
    		
    		for ($i = 10; $i <= $this->data->sheets[0]['numRows'] - 1; $i++) {
    			for ($j = 1; $j <= $this->data->sheets[0]['numCols']; $j++) {
    				$string = $this->data->sheets[0]['cells'][$i][$j];
    				if(($j==1 || $j==2) and $string != '-') {
    					$string = $this->xl2timestamp($string);
    					}
    			   if($j==3 ){
    					$string = trim($string);
    					$string = substr($string,0,-1);
    					$string = trim($string);
    					}
    				$this->data->sheets[0]['cells'][$i][$j] = $string;
    				
    			}
    			if($this->data->sheets[0]['cells'][$i][1] != '-'){
    				$line['date']= $this->data->sheets[0]['cells'][$i][1];
    			}
    			else{
    				$line['date'] = $this->data->sheets[0]['cells'][$i][2];
    			}
    			$line['description'] = preg_replace('/\s+/',' ',$this->data->sheets[0]['cells'][$i][3]);
    			$line['amount'] =  $this->data->sheets[0]['cells'][$i][5].$line['amount'] =  $this->data->sheets[0]['cells'][$i][7];;
    			if( $this->data->sheets[0]['cells'][$i][5] == ""){
    			$line['type'] = 'expense' ;
    			}
    			else
    			$line['type'] = 'income' ;
    			$line['note'] = $this->data->sheets[0]['cells'][$i][4]." ".$this->data->sheets[0]['cells'][$i][6];
    			$this->transaction[] = $line;
    			
    			
    			
    		
    		}
    		
    	}
}


class parse_bank_statement_BCS extends parse_bank_statement_base {
    private function xl2timestamp($xl_date) {
		  $timestamp =  date('Y-m-d',((int) $xl_date - 25569)*86400) ;
      return $timestamp;
		}
    
		public function read_content(){
		 		
    		
    		for ($i = 21; $i <= $this->data->sheets[0]['numRows'] - 5; $i++) {
    			for ($j = 1; $j <= $this->data->sheets[0]['numCols']; $j++) {
    				$string = $this->data->sheets[0]['cells'][$i][$j];
    				if(($j==1 || $j==2) and $string != '-') {
    					$string = $this->xl2timestamp($string);
    					}
    			   if($j==3 ){
    					$string = trim($string);
    					$string = substr($string,0,-1);
    					$string = trim($string);
    					}
    				$this->data->sheets[0]['cells'][$i][$j] = $string;
    				
    			}
    			if($this->data->sheets[0]['cells'][$i][1] != '-'){
    				$line['date']= $this->data->sheets[0]['cells'][$i][1];
    			}
    			else{
    				$line['date'] = $this->data->sheets[0]['cells'][$i][2];
    			}
    			$line['description'] = preg_replace('/\s+/',' ',$this->data->sheets[0]['cells'][$i][3]);
    			$line['amount'] =  $this->data->sheets[0]['cells'][$i][5];;
    			if( $this->data->sheets[0]['cells'][$i][4] == ""){
            if($line['description'] == "Addebito Saldo E/C Carta Di Credit" )
             $line['type'] = 'transfer';
    			   $line['type'] = 'expense' ;
             $line['amount'] =  $this->data->sheets[0]['cells'][$i][5];
    			}
    			else{
    			$line['type'] = 'income' ;
          $line['amount'] = "+".$this->data->sheets[0]['cells'][$i][4];
          }
    			$line['note'] =  preg_replace('/\s+/',' ',$this->data->sheets[0]['cells'][$i][6]);
    			$this->transaction[] = $line;
    			
    			
    			
    		
    		}
    		
    	}
}
?>